package org.example.footshoppingmaster.service.impl;

import org.example.footshoppingmaster.custom.ResultVO;
import org.example.footshoppingmaster.mapper.UserMapper;
import org.example.footshoppingmaster.pojo.DTO.LoginReturn;
import org.example.footshoppingmaster.service.UserService;
import org.example.footshoppingmaster.pojo.DO.User;
import org.example.footshoppingmaster.util.JwtUtils;
import org.example.footshoppingmaster.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

import static org.example.footshoppingmaster.custom.Constant.REFRESH_TOKEN_ACTIVE;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /*
     * 用户登录
     * */
    @Override
    public ResultVO<?> login(String email, String password) {
        User user=userMapper.getByEmailAndPassword(email,password);

        // 登录成功则分配令牌
        if (user!=null){
                String currentTime = new Date().toString();
                String loginToken = JwtUtils.creatToken(user.getName(), currentTime);
                //将用户名、当前时间和JWT存储到Redis中，以便后续的令牌刷新操作。
                RedisUtils.put(user.getName(), currentTime, REFRESH_TOKEN_ACTIVE);
                return ResultVO.success("登陆成功",new LoginReturn(user.getId(), "Bearer " + loginToken));
            }
        //登录失败, 返回错误信息
        return ResultVO.error("登陆失败，请重新输入");
    }
    /*
     * 用户注册
     * */
    @Override
    public String insertUser(String email,String password, String verificationCode) {
        //判断用户是否存在
        User selectUser=userMapper.getByEmail(email);
        if (selectUser==null){
//            用户不存在后的逻辑
            if (Objects.equals(RedisUtils.get(email), verificationCode)) {
                  //用户注册只需要 email name password
                 User user=new User();
                 user.setEmail(email);
                 user.setPassword(password);
                 user.setName("默认名称");
                 user.setSex("Mail");
                 user.setAge(18);
                 user.setSign("欢迎您！ 请重新设置您的签名！");
                 //默认头像 上传新头像时候会更新
                 user.setAvatar("http://sbo2jlg9j.hn-bkt.clouddn.com/ca3703c73c4c4f8780fdaf826d1bdb34.png?e=1712679929&token=3srnvznT7fa0SzDjuhLn2r0hdkUe2UZB17wpN1In:Eyvdvc-hHI3JzB0fw17uGabeL-A=");
                int res = userMapper.insert(user);
                if (res != 0) {
                    return "用户注册成功";
                } else {
                    return "用户注册失败";
                }
            }
            return "验证码错误";

        }
        return "当前用户已存在";
    }
    /*
    * 用户信息修改
    * */
    @Override
    public boolean updateUser(User user) {
        int res=userMapper.updateUser(user);
        return res != 0;
    }
    /*
    * 根据id查询用户信息
    * */
    @Override
    public User selectById(Integer id) {
        return userMapper.getById(id);
    }
    /*
    * 用户密码修改
    * */
    @Override
    public ResultVO<?> updatePassword(Integer id, String oldPwd, String newPwd) {

        if (userMapper.getById(id) == null) {
            return ResultVO.error("用户不存在");
        }
        if (!userMapper.getById(id).getPassword().equals(oldPwd)) {
            return ResultVO.error("旧密码错误");
        }
        userMapper.setPassword(id,oldPwd,newPwd);
        return ResultVO.success("密码修改成功");
    }
    /*
     * 头像上传存储到阿里云后 返回路径存到数据库
     * */
    @Override
    public ResultVO<?> updateAvatar(Integer id, String avatar) {
        if (userMapper.updateAvatar(id,avatar)){
            return ResultVO.success("头像更新成功");
        }
        return ResultVO.error("头像更新失败");
    }

}
